package com.workManager.controller;


import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.vo.ProcessDefinitionVo;
import com.workManager.vo.ProcessInstanceVo;
import com.workManager.vo.TaskVo;
import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Thinkpad on 2017/2/15 0015.
 */
@Controller
@RequestMapping("/Activiti")
public class ActivitiController extends BaseController{

  /*  @Autowired
    private ProcessEngine engine;


    *//**
     * 部署流程
     *//*
    @ApiOperation(value ="部署",notes = "项目部署")
    @RequestMapping(value="/deploy",method = RequestMethod.POST)
    public Object deploy( @RequestParam(value = "processName",required = true)String processName) {
        ModelMap modelMap = new ModelMap();
        ModelAndView mav = new ModelAndView();
        Map<String,Object> map = new HashMap<String, Object>();
        RepositoryService service = engine.getRepositoryService();
        if (null != processName)
            service.createDeployment().addClasspathResource("config/activiti/" + processName).deploy();
        List<ProcessDefinition> list = service.createProcessDefinitionQuery().list();
        System.out.println("部署流程-----------："+list);
        return setSuccessModelMap(modelMap,map);
    }

    *//**
     * 已部署流程
     *//*
    @RequestMapping(value = "/deployed",method = RequestMethod.POST)
    public  Object deployed(){
        ModelMap modelMap = new ModelMap();
        Map<String,Object> map = new HashMap<String, Object>();
        RepositoryService service = engine.getRepositoryService();
        List<ProcessDefinition> list = service.createProcessDefinitionQuery().list();
        System.out.println("已部署流程---------:"+list);
        List<ProcessDefinitionVo> processDefinitionVoList = new ArrayList<ProcessDefinitionVo>();
        for(ProcessDefinition processDefinition:list){
            ProcessDefinitionVo process =    new ProcessDefinitionVo();
            process.setId(processDefinition.getId());
            process.setCategory(processDefinition.getCategory());
            process.setDepploymentId(processDefinition.getDeploymentId());
            process.setDescription(processDefinition.getDescription());
            process.setTenantId(processDefinition.getTenantId());
            process.setKey(processDefinition.getKey());
            process.setResourceName(processDefinition.getResourceName());
            process.setSuspended(processDefinition.isSuspended());
            process.setHasStartFormKey(processDefinition.hasStartFormKey());
            process.setHasGraphicalNotation(processDefinition.hasGraphicalNotation());
            processDefinitionVoList.add(process);
        }

        map.put("list",processDefinitionVoList);
        return  setSuccessModelMap(modelMap,map);
    }

    *//**
     * 启动流程实例
     *//*
    @RequestMapping(value = "/start",method = RequestMethod.POST)
    public  Object start( @RequestParam(value = "key",required = true)String key){

        ModelMap modelMap = new ModelMap();
        Map<String,Object> map = new HashMap<String, Object>();
        RuntimeService service = engine.getRuntimeService();

      //  service.startProcessInstanceById(id);
        service.startProcessInstanceByKey(key);
        List<ProcessInstance> list = service.createProcessInstanceQuery().list();


      //  map.put("list",list);
        return  setSuccessModelMap(modelMap,map);
      //  return new ModelAndView("process/started");
    }

    *//**
     * 已启动流程实例
     *//*
    @RequestMapping(value = "/started",method = RequestMethod.POST)
    public Object started(){
        ModelMap modelMap = new ModelMap();
        Map<String,Object> map = new HashMap<String, Object>();
        RuntimeService service = engine.getRuntimeService();
        List<ProcessInstance> list = service.createProcessInstanceQuery().list();
        List<ProcessInstanceVo> processInstanceVoList = new ArrayList<ProcessInstanceVo>();
        for(ProcessInstance processInstance:list){
            ProcessInstanceVo processInstanceVo = new ProcessInstanceVo();
            processInstanceVo.setTenantId(processInstance.getTenantId());
            processInstanceVo.setSuspended(processInstance.isSuspended());
            processInstanceVo.setBusinessKey(processInstance.getBusinessKey());
            processInstanceVo.setDeploymentId(processInstance.getDeploymentId());
            processInstanceVo.setDescription(processInstance.getDescription());
            processInstanceVo.setLocalizedDescription(processInstance.getLocalizedDescription());
            processInstanceVo.setLocalizedName(processInstance.getLocalizedName());
            processInstanceVo.setName(processInstance.getName());
            processInstanceVo.setProcessDefinitionName(processInstance.getProcessDefinitionName());
            processInstanceVo.setProcessVariables(processInstance.getProcessVariables());
            processInstanceVo.setProcessDefinitionVersion(processInstance.getProcessDefinitionVersion());
            processInstanceVo.setProcessDefinitionKey(processInstance.getProcessDefinitionKey());
            processInstanceVoList.add(processInstanceVo);
        }

        map.put("list",processInstanceVoList);

        return  setSuccessModelMap(modelMap,map);
    }

    *//**
     * 任务列表
     *//*
    @RequestMapping(value = "/task",method = RequestMethod.POST)
    public  Object task (){
        ModelMap modelMap = new ModelMap();
        Map<String,Object> map = new HashMap<String, Object>();
        TaskService service = engine.getTaskService();
        List<Task> list = service.createTaskQuery().list();

        List<TaskVo> taskVoList = new ArrayList<TaskVo>();
        for(Task task:list){
            TaskVo taskVo = new TaskVo();
            taskVo.setTenantId(task.getTenantId());
            taskVo.setProcessVariables(task.getProcessVariables());
            taskVo.setAssignee(task.getAssignee());
            taskVo.setCategory(task.getCategory());
            taskVo.setCreateTime(task.getCreateTime());
            taskVo.setDescription(task.getDescription());
            taskVo.setDueDate(task.getDueDate());
            taskVo.setExecutionId(task.getExecutionId());
            taskVo.setFormKey(task.getFormKey());
            taskVo.setName(task.getName());
            taskVo.setOwner(task.getOwner());
            taskVo.setParentTaskId(task.getParentTaskId());
            taskVo.setCreateTime(task.getCreateTime());
            taskVo.setPriority(task.getPriority());
            taskVo.setTaskLocalVariables(task.getTaskLocalVariables());
            taskVo.setProcessInstanceId(task.getProcessInstanceId());
            taskVoList.add(taskVo);
        }

        map.put("list",taskVoList);

        return  setSuccessModelMap(modelMap,map);
    }

    *//**
     * 完成当前节点
     *//*
    @RequestMapping(value = "/complete",method = RequestMethod.POST)
    public Object complete(@RequestParam(value = "id",required = true)String id){
        ModelMap modelMap = new ModelMap();
        TaskService service = engine.getTaskService();
        service.complete(id);
        return  setSuccessModelMap(modelMap);
    }







    public void test(){

     ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();
        // 得到运行时服务组件
        RuntimeService runtimeService = engine.getRuntimeService();
        // 部署流程描述文件
        repositoryService.createDeployment()
                .addClasspathResource("classpath*//**//*config/activiti/audition.bpmn.xml").deploy();

        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("姓名", "程序员");
        variables.put("职务", "高级软件工程师");
        variables.put("语言", "Java/C#");
        variables.put("操作系统", "Window,Linux，unix，Aix");
        variables.put("工作地点","苏州高新技术软件园");


        // start process instance
        //获取创建一个实例
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("DeveloperWorkExam", variables);
        assert (pi!=null);

        List<Execution> executions = runtimeService.createExecutionQuery().list();
        assert (executions.size()==1);
        //执行开发技术知识面试业务
        Execution execution = runtimeService.createExecutionQuery().singleResult();
        runtimeService.setVariable(execution.getId(), "type", "receiveTask");
        runtimeService.signal(execution.getId());

        executions = runtimeService.createExecutionQuery().list();
        assert (executions.size()==1);
        //执行人事面试业务
        execution = executions.get(0);
        runtimeService.setVariable(execution.getId(), "oper", "录用此人....");
        runtimeService.signal(execution.getId());
    }*/
    public static void main(String[] args) {
        File file = new File("test.txt");
        System.out.println(file.isFile());

    }
}
